using System;
using System.Reflection;
using Castle.DynamicProxy;
using OWE.Core.Common.Response;
using OWE.Core.Log;
using OWE.Service.Configuration;

namespace OWE.Aspects
{
    public class ExceptionHandling : IInterceptor
    {
        #region IInterceptor Members

        public void Intercept(IInvocation invocation)
        {
            try
            {
                invocation.Proceed();
            }
            catch (Exception ex)
            {
                if (!ConfigManager.GetConfigValue<bool>("ExceptionEnabled", true))
                {
                    throw ex;
                }
                var log = new LogEntity
                              {
                                  ErrorMessage = ex.Message,
                                  StackTrace = ex.StackTrace,
                                  MethodResult = "Fail",
                                  MethodName = invocation.Method.Name,
                                  MethodParamXml = ex.ToString(),
                                  LogType = LogType.Service,
                                  LogSeverity = LogSeverity.Error
                              };

                if (invocation.Method.ReturnParameter.ParameterType.FullName == "Pandora.CommonTypes.Response.PrimitiveResponse")
                {
                    invocation.ReturnValue = new PrimitiveResponse
                                                 {
                                                     EntityPrimaryKey = null,
                                                     PrimitiveResponseValue = "",
                                                     ResponseCode = ResponseCodes.ServiceError,
                                                     ResponseErrorMessage = "Service Error",
                                                     ResponseMessage = "Service Error",
                                                     ResponseUserFriendlyMessageKey = "ResponseUserFriendlyMessageKey",
                                                     Log = log
                                                 };
                }
                else
                {
                    Type listType = invocation.Method.ReturnType;
                    object ret = null;
                    try
                    {
                        ret = Activator.CreateInstance(listType);
                        PropertyInfo responseCode = listType.GetProperty("ResponseCode");
                        responseCode.SetValue(ret, ResponseCodes.ServiceError, null);
                        PropertyInfo responseErrorMessage = listType.GetProperty("ResponseErrorMessage");
                        responseErrorMessage.SetValue(ret, "Service Error", null);
                        PropertyInfo responseMessage = listType.GetProperty("ResponseMessage");
                        responseMessage.SetValue(ret, "Service Error", null);
                        PropertyInfo logObj = listType.GetProperty("Log");
                        logObj.SetValue(ret, log, null);
                    }
                    catch
                    {
                    }
                    invocation.ReturnValue = ret;
                    //IoC.Resolve<ILogService>().InsertLog(log);
                }
            }
        }

        #endregion
    }
}